package com.panshi.controller;

import com.alibaba.fastjson.JSON;
import com.panshi.api.UserApi;
import com.panshi.vo.ItemQueryUserVo;
import com.panshi.vo.UserDetailResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.Map;

@Slf4j
@Controller
public class UserIndexController {

    @Autowired
    UserApi userApi;

    //个人主页（动态）
    @RequestMapping("/userHomepage/{id}")
    public ModelAndView personalHomepage(@PathVariable("id") Long id){
        ModelAndView modelAndView = new ModelAndView();

        ItemQueryUserVo itemQueryUserVo = new ItemQueryUserVo();
        itemQueryUserVo.setId(id);
        itemQueryUserVo.setType(100);

        Map<String, Object> map = userApi.userIndex(itemQueryUserVo).getData();

        Object userMemberStr = map.get("userMemberOne");

        UserDetailResult result = JSON.parseObject(userMemberStr.toString(), UserDetailResult.class);

        modelAndView.addObject("check","ok");

        modelAndView.addObject("userMember",result.getUserMember());

        modelAndView.addObject("mapData",result.getCountList());

        modelAndView.setViewName("userHomepage");

        return modelAndView;
    }
    //个人中心（动态）
    @RequestMapping("/userCenterDynamicState/{id}")
    public ModelAndView dyList(@PathVariable("id") Long id,@RequestParam("current") Integer current){
        ModelAndView modelAndView = new ModelAndView();

        ItemQueryUserVo itemQueryUserVo = new ItemQueryUserVo();
        itemQueryUserVo.setId(id);
        itemQueryUserVo.setType(100);
        itemQueryUserVo.setCurrent(current);

        Map<String, Object> map = userApi.dyList(itemQueryUserVo).getData();

        Object userMemberStr = map.get("userDynamicRelevanceList");

        UserDetailResult result = JSON.parseObject(userMemberStr.toString(), UserDetailResult.class);

        modelAndView.addObject("userDynamicRelevanceLists",result.getUserDynamicRelevanceList());

        modelAndView.addObject("check","ok");

        modelAndView.setViewName("userDynamicList");

        return modelAndView;
    }
    //个人中心（问题）
    @RequestMapping("/userCenterQuiz/{id}")
    public ModelAndView qalist(@PathVariable("id") Long id,@RequestParam("current") Integer current){
        ModelAndView modelAndView = new ModelAndView();

        ItemQueryUserVo itemQueryUserVo = new ItemQueryUserVo();
        itemQueryUserVo.setId(id);
        itemQueryUserVo.setType(100);
        itemQueryUserVo.setCurrent(current);

        Map<String, Object> map = userApi.qaList(itemQueryUserVo).getData();

        Object userMemberStr = map.get("qaQuestionList");

        modelAndView.addObject("check","ok");

        UserDetailResult result = JSON.parseObject(userMemberStr.toString(), UserDetailResult.class);
        modelAndView.addObject("userDynamicRelevanceQuestionList",result.getUserDynamicQuestionList());

        modelAndView.setViewName("userDynamicList");

        return modelAndView;
    }
    //个人中心（回答）
    @RequestMapping("/userCenterAnswer/{id}")
    public ModelAndView qaReplylist(@PathVariable("id") Long id,@RequestParam("current") Integer current){
        ModelAndView modelAndView = new ModelAndView();

        ItemQueryUserVo itemQueryUserVo = new ItemQueryUserVo();
        itemQueryUserVo.setId(id);
        itemQueryUserVo.setType(100);
        itemQueryUserVo.setCurrent(current);

        Map<String, Object> map = userApi.answerList(itemQueryUserVo).getData();

        Object userMemberStr = map.get("qaAnswerList");

        UserDetailResult result = JSON.parseObject(userMemberStr.toString(), UserDetailResult.class);

        modelAndView.addObject("check","ok");

        modelAndView.addObject("userDynamicRelevanceAnswerList",result.getUserDynamicAnswerList());

        modelAndView.setViewName("userDynamicList");

        return modelAndView;
    }
    //个人中心（资讯）
    @RequestMapping("/userCenterArticle/{id}")
    public ModelAndView articlelist(@PathVariable("id") Long id,@RequestParam("current") Integer current){
        ModelAndView modelAndView = new ModelAndView();

        ItemQueryUserVo itemQueryUserVo = new ItemQueryUserVo();
        itemQueryUserVo.setId(id);
        itemQueryUserVo.setType(100);
        itemQueryUserVo.setCurrent(current);

        Map<String, Object> map = userApi.zxList(itemQueryUserVo).getData();

        Object userMemberStr = map.get("zxList");

        modelAndView.addObject("check","ok");

        UserDetailResult result = JSON.parseObject(userMemberStr.toString(), UserDetailResult.class);

        modelAndView.addObject("userDynamicRelevanceZxList",result.getUserDynamicZxList());

        modelAndView.setViewName("userDynamicList");

        return modelAndView;
    }

}
